package cn.topevery.garden.dal.util;

import cn.topevery.garden.dal.dao.GreFileDao;
import cn.topevery.garden.entity.po.GreFile;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class FileUtils {
    @Autowired
    GreFileDao greFileDao;

    public  void saveFile(List<GreFile> files, Integer id, String userId,Integer type){
        greFileDao.deleteByMainId(id, userId, type);
        if (files != null && !files.isEmpty()) {
            List<GreFile> collect = files.stream().peek(temp -> {
                temp.setDataType(type);
                temp.setMainId(id);
                temp.setMainRecordId(id);
                temp.setDbStatus(false);
                temp.setFileSuffix(StringUtils.isNotBlank(temp.getFileSuffix()) ? temp.getFileSuffix() : temp.getFileName().substring(temp.getFileName().lastIndexOf(".")));
            }).collect(Collectors.toList());
            for (GreFile greFile : collect) {
                greFileDao.saveOrUpdate(userId, greFile);
            }
        }
    }
    public List<GreFile>  getFile(Integer id, Integer type){

        return greFileDao.getListByMainRecordId(id,type);
    }

}

